package com.mymarkdown.app.util;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * @author zenglun
 * @date 2021/12/15 9:53 上午
 * @description: fx 弹出窗工具类
 */
public class FxShowWindowUtil {

    private WebView webView;

    private WebEngine engine;
    /**
     * 要加载显示的页面
     */
    private String url;
    /**
     * 要显示的标题
     */
    private String title;
    /**
     * 窗口最小宽度
     */
    private int minWidth;
    /**
     * 窗口最小高度
     */
    private int minHeight;

    /**
     * 构建一个空对象,并设置相关加载的页面URL
     * @param url 要加载显示的URL页面
     * @return
     */
    public static FxShowWindowUtil empty(String url){
        FxShowWindowUtil fxShowWindowUtil = new FxShowWindowUtil();
        fxShowWindowUtil.webView = new WebView();
        fxShowWindowUtil.engine = fxShowWindowUtil.webView.getEngine();
        fxShowWindowUtil.engine.load(url);
        return fxShowWindowUtil;
    }

    /**
     * 设置相关的标题
     * @param title
     * @return
     */
    public FxShowWindowUtil setTitle(String title) {
        this.title = title;
        return this;
    }
    /**
     * 设置最小宽度
     * @param minWidth
     * @return
     */
    public FxShowWindowUtil setMinWidth(int minWidth) {
        this.minWidth = minWidth;
        return this;
    }

    /**
     * 设置最小高度
     * @param minHeight
     * @return
     */
    public FxShowWindowUtil setMinHeight(int minHeight) {
        this.minHeight = minHeight;
        return this;
    }

    private FxShowWindowUtil(){}
    /**
     * 显示一个window窗体,该窗体为模态,窗体，并禁用最大化，最小化按钮
     */
    public void showWindow(){
        Stage window = new Stage();
        window.setTitle(title);
        window.initModality(Modality.APPLICATION_MODAL);
        window.setWidth(minWidth);
        window.setHeight(minHeight);
        VBox layout = new VBox(10);
        layout.setAlignment(Pos.CENTER);
        layout.getChildren().add(webView);
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.resizableProperty().setValue(Boolean.FALSE);
        window.showAndWait();
        webView.prefHeightProperty().bind(layout.heightProperty());
        webView.prefWidthProperty().bind(layout.widthProperty());
    }
}
